Add ForwardSlashPath branded type and forward-slash path utilities#1853
Add ForwardSlashPath branded type and forward-slash path utilities#1853pmcelhaney merged 4 commits intomainfrom
ForwardSlashPath branded type and forward-slash path utilities#1853Conversation
ForwardSlashPath branded type and toForwardSlashPath utility
pmcelhaney
left a comment
There was a problem hiding this comment.
The new function is mostly used to fix the output of nodePath.join() or nodePath.relative()
Create two new utility functions that wrap the nodePath functions so we can eliminate duplication.
Done in commit
All call sites updated; several files no longer import |
ForwardSlashPath branded type and toForwardSlashPath utilityForwardSlashPath branded type and forward-slash path utilities
pmcelhaney
left a comment
There was a problem hiding this comment.
Remove the unit test that's failing on Windows. It's not technically correct.
Head branch was pushed to by a user without write access
Removed in commit |
Summary
Centralizes all
replaceAll("\\", "/")path normalization behind a suite of utility functions that return a brandedForwardSlashPathtype, enabling static enforcement that backslash-to-forward-slash conversion has occurred before a path is used in contexts that require it (chokidar watchers, ES module specifiers, URL routing).New utility (
src/util/forward-slash-path.ts):ForwardSlashPath— branded type (string & { readonly [__forwardSlashPath]: never }) using aunique symbolto prevent accidental assignment of un-normalized stringstoForwardSlashPath(path: string): ForwardSlashPath— the core normalization functionpathJoin(...paths: string[]): ForwardSlashPath— wrapsnodePath.join, returning a forward-slash pathpathRelative(from: string, to: string): ForwardSlashPath— wrapsnodePath.relative, returning a forward-slash pathpathDirname(path: string): ForwardSlashPath— wrapsnodePath.dirname, returning a forward-slash pathpathResolve(...paths: string[]): ForwardSlashPath— wrapsnodePath.resolve, returning a forward-slash pathUpdated call sites — 13 files replaced inline
.replaceAll("\\", "/")andtoForwardSlashPath(nodePath.xxx(...))patterns with the new utilities:src/server/—file-discovery.ts,module-loader.ts,transpiler.tssrc/typescript-generator/—repository.ts,script.ts,generate.ts,operation-coder.ts,operation-type-coder.ts,parameters-type-coder.ts,prune.tssrc/migrate/update-route-types.ts,src/app.ts,bin/counterfact.jsSeveral files (
transpiler.ts,script.ts,operation-coder.ts,operation-type-coder.ts,parameters-type-coder.ts,file-discovery.ts,app.ts) no longer importnodePathat all.Test updates:
test/server/transpiler.test.ts— removed localforwardSlashhelper; now importstoForwardSlashPathtest/util/forward-slash-path.test.ts— 13 unit tests covering all exported functions; removed thepathResolve › returns an absolute pathassertion which was platform-dependent (Windows prepends a drive letter, making the expected value technically incorrect)Original Prompt
Create a utility function to replace replaceAll("\", "/") throughout the code.
Return a branded string and use that branded string wherever a path without backslashes is needed.
Manual acceptance tests
toForwardSlashPath("C:\\Users\\foo\\bar")returns"C:/Users/foo/bar"with no backslashes remainingpathJoin("routes", "pets.ts")returns"routes/pets.ts"(forward slashes, cross-platform)pathRelative("/a/b", "/a/b/c")returns"c"(forward slashes, cross-platform)stringto aForwardSlashPath-typed variable without going through one of the utility functionsTasks
src/util/forward-slash-path.tswithForwardSlashPathbranded type,toForwardSlashPath, and fournodePath-wrapping helpers (pathJoin,pathRelative,pathDirname,pathResolve).replaceAll("\\", "/")occurrences across 13 source files withtoForwardSlashPathor the appropriate wrappertoForwardSlashPath(nodePath.xxx(...))call-site patterns with the corresponding wrapper functiontest/server/transpiler.test.tsto importtoForwardSlashPathinstead of using a local helpertest/util/forward-slash-path.test.tswith 13 unit tests covering all exported functionspathResolveabsolute-path test that failed on Windows (drive letter is prepended, making the expected value incorrect)